package com.heypixel.fixDupeTrident;

import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.projectiles.ProjectileSource;
import org.jetbrains.annotations.NotNull;

public final class FixDupeTrident extends JavaPlugin implements @NotNull Listener {

    @Override
    public void onEnable() {
        // Plugin startup logic

        getServer().getPluginManager().registerEvents(this, this);

    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }


    @EventHandler
    public void onTrident(ProjectileLaunchEvent event) {
        Projectile entity = event.getEntity();
        ProjectileSource shooter = entity.getShooter();
        if (shooter instanceof Player player) {
            Inventory topInventory = player.getOpenInventory().getTopInventory();
            Inventory bottom = player.getOpenInventory().getBottomInventory();
            if (!topInventory.getType().equals(InventoryType.CRAFTING) && !bottom.getType().equals(InventoryType.PLAYER)) {
                getLogger().warning(String.format("[DupeTrident] 检测到玩家 %s - %s 在开启界面时发射 %s 。上方界面: %s, 下方界面: %s", player.getName(), player.getUniqueId(), entity.getType(), topInventory.getType(), bottom.getType()));
                event.setCancelled(true);
                return;
            }

            ItemStack[] contents = topInventory.getContents();
            for (ItemStack content : contents) {
                if (!content.getType().isAir()) {
                    getLogger().warning(String.format("[DupeTrident] 检测到玩家 %s - %s 在发射 %s 时，合成栏存在 %s。", player.getName(), player.getUniqueId(), entity.getType(), content.getType()));
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
}
